<?php
$get = [];
$get['project'] = function ($cmd, $token) {
  $tmp = ['table' => 'project'];
  if (isset($cmd['id'])) $tmp['id'] = $cmd['id'];
  if (isset($cmd['pid'])) $tmp['pid'] = $cmd['pid'];
  if (isset($cmd['name'])) $tmp['name'] = $cmd['name'];
  if (isset($cmd['info'])) $tmp['info'] = $cmd['info'];
  if (isset($cmd['other'])) $tmp['other'] = $cmd['other'];
  if (isset($cmd['userType'])) $tmp['userType'] = $cmd['userType'];

  if ($token['type'] != 管理员) {
    $tmp['userType'] = $token['uts'];
  }
  $tmp = GetData($tmp);
  if ($tmp['error']) $tmp['data'] = $cmd;
  else {
    foreach ($tmp as $key => $value) {
      if (isset($value['process']) && is_string($value['process'])) $value['process'] = json_decode($value['process'], true);
      $tmp[$key] = $value;
    }

    $tmp = ['result' => $tmp];
  }
  return $tmp;
};

$get['itemIO'] = function ($cmd, $token) {
  $tmp = ['table' => strtolower('itemIO')];
  $other = '';
  if (isset($cmd['id'])) $tmp['id'] = $cmd['id'];
  if (isset($cmd['time'])) $other = 'CreateTime>' . $cmd['time'][0] . ' and CreateTime<' . $cmd['time'][1];
  if (isset($cmd['type'])) $tmp['type'] = $cmd['type'];
  if (isset($cmd['user'])) $tmp['user'] = $cmd['user'];
  if (isset($cmd['state'])) $tmp['state'] = $cmd['state'];
  if (isset($cmd['item'])) $tmp['info'] = ' like %"id":' . (int) $cmd['item'] . '%';
  // return setVal('info',$tmp['info'],false,true);

  if ($token['type'] != 管理员 && $token['type'] != 巡查员) {
    $tmp['user'] = $token['id'];
  }
  if ($other) {
    if (count(array_keys($tmp)) > 1) {
      $other = ' and ' . $other;
    } else {
      $other = ' where ' . $other;
    }
  }
  $tmp = GetData($tmp, '*', false, 'and', $other);
  if (!isset($tmp['error'])) {
    foreach ($tmp as $key => $value) {
      if (isset($value['info']) && is_string($value['info'])) $value['info'] = json_decode($value['info'], true);
      $value['user'] = GetData(['table' => 'users', 'id' => $value['user']], 'name', true);

      $tmp[$key] = $value;
    }
    $tmp = ['result' => $tmp];
  }
  return $tmp;
};
